home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-11-08 | 13.3 KB | 474 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: FWEdCmd.cpp
- // Release Version: $ 1.0d11 $
- //
- // Copyright: © 1995 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "FWFrameW.hpp"
-
- #ifndef FWEDCMD_H
- #include "FWEdCmd.h"
- #endif
-
- // ----- Framework Includes -----
-
- #ifndef FWPART_H
- #include "FWPart.h"
- #endif
-
- #ifndef FWFRAME_H
- #include "FWFrame.h"
- #endif
-
- #ifndef FWSELECT_H
- #include "FWSelect.h"
- #endif
-
- #ifndef FWLNKMGR_H
- #include "FWLnkMgr.h"
- #endif
-
- // ----- OS Layer -----
-
- #ifndef FWBARRAY_H
- #include "FWBArray.h"
- #endif
-
- // ----- OpenDoc Includes -----
-
- #ifndef SOM_Module_OpenDoc_Commands_defined
- #include <CmdDefs.xh>
- #endif
-
- #ifndef SOM_ODSession_xh
- #include <ODSessn.xh>
- #endif
-
- #ifndef SOM_ODClipboard_xh
- #include <Clipbd.xh>
- #endif
-
- #ifndef SOM_Module_OpenDoc_StdProps_defined
- #include <StdProps.xh>
- #endif
-
- #ifndef SOM_Module_OpenDoc_StdTypes_defined
- #include <StdTypes.xh>
- #endif
-
- #ifndef SOM_ODStorageUnit_xh
- #include <StorageU.xh>
- #endif
-
- #ifndef SOM_ODLinkSpec_xh
- #include <LinkSpec.xh>
- #endif
-
- //========================================================================================
- // Runtime Info
- //========================================================================================
-
- #if FW_LIB_EXPORT_PRAGMAS
- #pragma lib_export on
- #endif
-
- #ifdef FW_BUILD_MAC
- #pragma segment FWFrameworkCommands
- #endif
-
- FW_DEFINE_CLASS_M1(FW_CEditCommand, FW_CCommand)
-
- //====================================================================
- // FW_CEditCommand class
- //====================================================================
-
- //--------------------------------------------------------------------
- // FW_CEditCommand constructor
- //--------------------------------------------------------------------
-
- FW_CEditCommand::FW_CEditCommand(Environment* ev,
- ODCommandID id,
- FW_CFrame* frame,
- FW_Boolean canUndo) :
- FW_CCommand(ev, id, frame, canUndo),
- fUpdateID(kODUnknownUpdate),
- fCloneKind(kODCloneCopy)
- {
- switch (id)
- {
- case kODCommandCopy:
- fCanUndo = FALSE; // Copy is never Undo-able
- fCausesChange = FALSE; // Copy doesn't change the data
- break;
- case kODCommandCut:
- fCloneKind = kODCloneCut;
- break;
- case kODCommandPaste:
- case kODCommandPasteAs:
- fCloneKind = kODClonePaste;
- break;
- }
- }
-
- //--------------------------------------------------------------------
- // FW_CEditCommand destructor
- //--------------------------------------------------------------------
-
- FW_CEditCommand::~FW_CEditCommand()
- {
- }
-
- //--------------------------------------------------------------------
- // FW_CEditCommand::DoIt
- //--------------------------------------------------------------------
-
- void FW_CEditCommand::DoIt(Environment* ev) // Override
- {
- FW_Boolean result = FALSE;
-
- if (fCanUndo)
- this->SaveUndoState(ev); // save current state, for later Undo
-
- switch (fCommandID)
- {
- case kODCommandCopy:
- this->HandleCopy(ev);
- break;
-
- case kODCommandClear:
- if (this->IsOKtoEdit(ev) && this->HandleClear(ev))
- {
- this->SetMenuStrings(ev, "Undo Clear", "Redo Clear");
- result = TRUE;
- }
- break;
-
- case kODCommandCut:
- if (this->IsOKtoEdit(ev) && this->HandleCut(ev))
- {
- this->SetMenuStrings(ev, "Undo Cut", "Redo Cut");
- result = TRUE;
- }
- break;
-
- case kODCommandPaste:
- if (this->IsOKtoEdit(ev) && this->HandlePaste(ev))
- {
- this->SetMenuStrings(ev, "Undo Paste", "Redo Paste");
- result = TRUE;
- }
- break;
-
- case kODCommandPasteAs:
- if (this->IsOKtoEdit(ev) && this->HandlePasteAs(ev))
- {
- this->SetMenuStrings(ev, "Undo Paste As", "Redo Paste As");
- result = TRUE;
- }
- break;
- }
-
- if (result == FALSE)
- fCanUndo = FALSE;
- else if (fCanUndo)
- {
- this->SaveRedoState(ev); // save new state, for later Redo
- }
- }
-
-
- //----------------------------------------------------------------------------------------
- // FW_CEditCommand::DoCut
- //----------------------------------------------------------------------------------------
-
- void FW_CEditCommand::DoCut(Environment* ev)
- {
- // User may override; only gets called if the Cut was successful
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditCommand::DoClear
- //----------------------------------------------------------------------------------------
-
- void FW_CEditCommand::DoClear(Environment* ev)
- {
- // User may override; only gets called if the Clear was successful
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditCommand::DoPaste
- //----------------------------------------------------------------------------------------
-
- void FW_CEditCommand::DoPaste(Environment* ev)
- {
- // User may override; only gets called if the Paste was successful
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditCommand::DoPasteAs
- //----------------------------------------------------------------------------------------
-
- void FW_CEditCommand::DoPasteAs(Environment* ev)
- {
- // User may override; only gets called if the Paste As was successful
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditCommand::HandleCut
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CEditCommand::HandleCut(Environment* ev)
- {
- FW_Boolean result = FALSE;
-
- this->Copy(ev, FALSE); // don't write a link spec for a cut
- result = fSelection->ClearSelection(ev);
-
- if (result)
- {
- // Give the command subclass a chance to do something
- this->DoCut(ev);
- }
-
- return result;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditCommand::HandleCopy
- //----------------------------------------------------------------------------------------
-
- void FW_CEditCommand::HandleCopy(Environment* ev)
- {
- FW_Boolean allowPublish = (fSelection && fSelection->IsSelectionPublishable(ev));
- this->Copy(ev, allowPublish);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditCommand::HandleClear
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CEditCommand::HandleClear(Environment* ev)
- {
- FW_Boolean result = fSelection->ClearSelection(ev);
- if (result)
- this->DoClear(ev); // Give the command subclass a chance to do something
-
- return result;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditCommand::Copy
- //----------------------------------------------------------------------------------------
-
- void FW_CEditCommand::Copy(Environment* ev, FW_Boolean allowLinking)
- {
- //---- We have to invalidate any currently pending publisher ----
- FW_CLinkManager* linkMgr = fPart->GetLinkManager(ev);
- if (linkMgr)
- linkMgr->DeletePendingPublish(ev);
-
- //---- Clear all properties and values from the clipboard ----
- ODClipboard* clipboard = fPart->GetSession(ev)->GetClipboard(ev);
- clipboard->Clear(ev);
- ODStorageUnit* clipboardSU = clipboard->GetContentStorageUnit(ev);
-
- //---- Write new data to the clipboard ----
- fSelection->ExternalizeData(ev, fFrame, FW_kClipboardStorage, clipboardSU, fCloneKind);
-
- //---- notify OpenDoc clipboard ----
- fUpdateID = clipboard->ActionDone(ev, fCloneKind);
-
- // ----- Write out Link Spec if possible -----
- if (allowLinking)
- {
- ODLinkSpec* linkSpec = NULL;
- FW_VOLATILE(linkSpec);
- ODUpdateID updateID = clipboard->GetUpdateID(ev);
- FW_CByteArray data(&updateID, sizeof(updateID));
-
- FW_TRY
- {
- linkSpec = fPart->GetStorageUnit(ev)->GetDraft(ev)->CreateLinkSpec(ev, fPart->GetODPart(ev), data);
-
- clipboardSU->AddProperty(ev, kODPropLinkSpec);
- linkSpec->WriteLinkSpec(ev, clipboardSU);
- }
- FW_CATCH_BEGIN
- FW_CATCH_EVERYTHING()
- {
- if (linkSpec)
- delete linkSpec;
- FW_THROW_SAME();
- }
- FW_CATCH_END
-
- delete linkSpec;
-
- // If current selection is already published, just re-use its publisher
- FW_CPublishLink* publishLink = fSelection->DoFindPublisher(ev);
- if (publishLink)
- publishLink->SetPendingID(ev, updateID);
- else
- publishLink = linkMgr->NewPublishLink(ev, updateID, fFrame->GetPresentation(ev));
- linkMgr->SetPendingPublish(ev, publishLink);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditCommand::HandlePaste
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CEditCommand::HandlePaste(Environment* ev)
- {
- FW_Boolean result = FALSE;
-
- ODClipboard* clipboard = fPart->GetSession(ev)->GetClipboard(ev);
- ODStorageUnit* clipBSU = clipboard->GetContentStorageUnit(ev);
-
- result = fSelection->InternalizeData(ev, fFrame, clipBSU, fCloneKind) != FW_kInternalizeFailed;
-
- if (result)
- {
- // notify OpenDoc clipboard
- fUpdateID = clipboard->ActionDone(ev, fCloneKind);
-
- // Give the command subclass a chance to do something
- this->DoPaste(ev);
- }
-
- return result;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditCommand::HandlePasteAs
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CEditCommand::HandlePasteAs(Environment* ev)
- {
- FW_Boolean result = FALSE;
- FW_Boolean handledIt = FALSE;
- ODPasteAsResult pasteAsResult;
-
- result = this->HandlePasteAsDialog(ev, pasteAsResult, handledIt);
- if (!handledIt)
- {
- if ((pasteAsResult.translateKind != kODNULL)) // user wants data translated
- result = this->DoPasteTranslation(ev, pasteAsResult);
- else
- {
- fCommandID = kODCommandPaste;
- result = this->HandlePaste(ev); // no translation, just an ordinary paste
- }
- }
- else if (result)
- {
- // Give the command subclass a chance to do something
- this->DoPasteAs(ev);
- }
-
- return result;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditCommand::HandlePasteAsDialog
- //----------------------------------------------------------------------------------------
- FW_Boolean FW_CEditCommand::HandlePasteAsDialog(Environment* ev, ODPasteAsResult& pasteAsResult,
- FW_Boolean& handledIt)
- {
- FW_Boolean result = FALSE;
- ODPasteAsMergeSetting mergeSetting;
- ODBoolean canPasteLink = FALSE;
- FW_CLinkManager* linkMgr = fPart->GetLinkManager(ev);
-
- ODClipboard* clipboard = fPart->GetSession(ev)->GetClipboard(ev);
- ODStorageUnit* clipboardSU = clipboard->GetContentStorageUnit(ev);
-
- // ---- Check whether the selection supports linking ----
- if (linkMgr && fSelection->CanSubscribe(ev, mergeSetting))
- {
- /* if (clipboardSU->Exists(ev, kODPropLinkSpec, (ODValueType)NULL, 0)) [MEB] OpenDoc calls this */
- canPasteLink = TRUE;
- }
-
- // ---- Display the Paste As... dialog ----
- result = clipboard->ShowPasteAsDialog(ev, canPasteLink, mergeSetting,
- fFrame->GetActiveFacet(ev), // facet from which dialog is triggered
- fFrame->GetViewType(ev), // viewType of data
- &pasteAsResult);
-
- if (result == kODFalse)
- handledIt = TRUE; // we tried, but the user cancelled
- else
- {
- // --- Figure out what to do depending on settings from Paste As dialog
- if (pasteAsResult.pasteLinkSetting == kODTrue) // user wants to create a Link
- {
- result = linkMgr->PasteWithLink(ev, clipboardSU, pasteAsResult, fFrame->GetPresentation(ev));
- handledIt = TRUE;
- }
- else if (pasteAsResult.mergeSetting == kODFalse) // user wants to embed a Part w/o Linking
- {
- result = this->DoPasteAsEmbed(ev, clipboardSU);
- handledIt = TRUE;
- }
- else // Merge with Contents - let Paste command handle that
- {
- handledIt = FALSE;
- }
- }
-
- return result;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditCommand::DoPasteTranslation
- //----------------------------------------------------------------------------------------
- FW_Boolean FW_CEditCommand::DoPasteTranslation(Environment* ev, ODPasteAsResult& pasteAsResult)
- {
- // Override to translate the data and paste it.
- // Use the clipboard content SU.
-
- return FALSE;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditCommand::DoPasteAsEmbed
- //----------------------------------------------------------------------------------------
- FW_Boolean FW_CEditCommand::DoPasteAsEmbed(Environment* ev, ODStorageUnit* storageUnit)
- {
- FW_UNUSED(storageUnit);
-
- // Override to paste the contents of the storageUnit as an embedded part (no linking)
- return FALSE;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditCommand::UndoIt
- //----------------------------------------------------------------------------------------
-
- void FW_CEditCommand::UndoIt(Environment* ev) // Override
- {
- if ((fCommandID == kODCommandCut) || (fCommandID == kODCommandPaste))
- {
- ODClipboard* clipboard = fPart->GetSession(ev)->GetClipboard(ev);
- clipboard->ActionUndone(ev, fUpdateID, fCloneKind);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditCommand::RedoIt
- //----------------------------------------------------------------------------------------
-
- void FW_CEditCommand::RedoIt(Environment* ev) // Override
- {
- if ((fCommandID == kODCommandCut) || (fCommandID == kODCommandPaste))
- {
- ODClipboard* clipboard = fPart->GetSession(ev)->GetClipboard(ev);
- clipboard->ActionRedone(ev, fUpdateID, fCloneKind);
- }
- }
-
-